package com.tonghaibo.controller;

import com.sun.org.apache.bcel.internal.classfile.Code;
import com.tonghaibo.expection.BusinessException;
import com.tonghaibo.po.Book;
import com.tonghaibo.service.BookService;
import com.tonghaibo.vo.Result;
import com.tonghaibo.vo.ResultCode;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jitwxs
 * @date 2025年04月27日 22:48
 */
//@Component
//@ResponseBody
@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private BookService bookService;

    @GetMapping
    public Result test1(){
        //try {
        //    int i=1/0;
        //} catch (Exception e) {
        //    throw new BusinessException(ResultCode.BUSINESSEXCEPTION,"不要瞎弄我看着呢");
        //}
        List<Book> all = bookService.findAll();
        Integer i=all!=null?ResultCode.SELECT_OK:ResultCode.SELECT_ERR;
        String msg=i==ResultCode.SELECT_OK?"":"查询失败";
        return new Result(i,msg,all);
    }
    @PostMapping
    public Result  test2(@RequestBody Book book){
        boolean inster1 = bookService.inster(book);
        return new Result(inster1?ResultCode.INSTRET_OK:ResultCode.INSTRET_ERR,inster1) ;
    }

    @GetMapping("/{id}")
    public Result test3(@PathVariable Integer id){
        Book books = bookService.findById(id);
        Integer integer = books != null ? ResultCode.SELECTID_OK : ResultCode.SELECTID_ERR;
        String s = books != null ? "" : "数据查询失败，请重试！！";
        return new Result(integer,s,books);
    }

    @DeleteMapping("/{id}")
    public Result test4(@PathVariable Integer id){
        boolean b = bookService.delectById(id);
        return new Result(b?ResultCode.DELECT_OK:ResultCode.DELECT_ERR,b);
    }
}
